package com.yuerlive.rainbow.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.utils.UserProxy;
import com.yuerlive.rainbow.widget.timeselector.TextUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admin on 2017/5/26.
 */

public class ShareDialog extends Dialog implements UserProxy.OnUserChangeListener {
    @BindView(R.id.llSina)
    LinearLayout llSina;
    @BindView(R.id.llFriend)
    LinearLayout llFriend;
    @BindView(R.id.llWeChat)
    LinearLayout llWeChat;
    @BindView(R.id.llQQ)
    LinearLayout llQQ;
    @BindView(R.id.tvCancel)
    TextView tvCancel;
    @BindView(R.id.rlexpertMoreView)
    LinearLayout rlexpertMoreView;

    @Nullable
    @BindView(R.id.imgQcCode)
    RoundImageView imgQcCode;
    @Nullable
    @BindView(R.id.tvNickName)
    TextView tvNickName;
    @Nullable
    @BindView(R.id.tvID)
    TextView tvID;

    @Nullable
    @BindView(R.id.rlUser)
    RelativeLayout rlUser;

    @Nullable
    @BindView(R.id.imgCover)
    RoundImageView imgCover;


    private Context context;
    private View shareView;


    public ShareDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        init(false);
    }

    public ShareDialog(Context context, int themeResId, boolean withUser) {
        super(context, themeResId);
        this.context = context;
        init(withUser);
    }

    private void init(boolean withUser) {
        if (withUser) {
            shareView = LayoutInflater.from(context).inflate(R.layout.share_dialog_withuser, null);
            ButterKnife.bind(this, shareView);
            initUserInfo();
            rlUser.setDrawingCacheEnabled(true);
            rlUser.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            UserProxy.addListener(this);
        } else {
            shareView = LayoutInflater.from(context).inflate(R.layout.share_dialog, null);
            ButterKnife.bind(this, shareView);
        }


        this.setContentView(shareView);
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.BOTTOM;
        this.getWindow().setAttributes(lp);
        this.getWindow().setWindowAnimations(R.style.shareDialog);
        this.setCanceledOnTouchOutside(true);
        tvCancel.setOnClickListener((v) -> {
            dismiss();
        });
    }

    public void setOnItemClick(View.OnClickListener itemClick) {
        llSina.setOnClickListener(itemClick);
        llWeChat.setOnClickListener(itemClick);
        llFriend.setOnClickListener(itemClick);
        llQQ.setOnClickListener(itemClick);
    }

    private void initUserInfo() {
        User user = UserProxy.getUser();
        if(!TextUtil.isEmpty(user.getUserInfo().getNickname())) {
            tvNickName.setText(user.getUserInfo().getNickname());
            if(user.getUserInfo().getSex()==0){
                tvNickName.setCompoundDrawablesWithIntrinsicBounds(null,null,context.getResources().getDrawable(R.drawable.icon_boy),null);
            }else{
                tvNickName.setCompoundDrawablesWithIntrinsicBounds(null,null,context.getResources().getDrawable(R.drawable.icon_girl),null);
            }
        }else{
            tvNickName.setVisibility(View.INVISIBLE);
        }
        tvID.setText("ID: " + user.getUserId());
        GlideUtil.load(context, user.getUserInfo().getIcon(), imgCover);
        GlideUtil.load(context, user.getUserInfo().getQrUrl(), imgQcCode);
    }

    public Bitmap getImageBitmap() {
        return rlUser.getDrawingCache();
    }

    @Override
    public void dismiss() {
        isShowing = false;
        super.dismiss();
        if (rlUser != null) {
            UserProxy.removeListener(this);
        }
    }


    @Override
    public void onUserChange(User user) {
        if (user != null) {
            initUserInfo();
        }
    }

    @Override
    public void show() {
        isShowing = true;
        if (rlUser != null)
            initUserInfo();
        super.show();
    }

    @Override
    public void hide() {
        isShowing = false;
        super.hide();
    }


    @Override
    public boolean isShowing() {
        return isShowing;
    }

    private boolean isShowing;
}
